#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
 Jexjex
 Copyright Jan-Karel Visser - all rights are reserved
 Licensed under the LGPL v3

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

"""


class Jexjex_Tickets:

    def __init__(self, jexjex):
        self.app = jexjex

    def add_ticket(self, refer=None, message=None):
        self.jexjex_tickets.insert(refer=refer, message=message)
        self.app.db.commit()

    def get_all(self, refer=None, message=None):
        row = self.app.db(self.jexjex_tickets).select(
                         self.jexjex_tickets.id,
                         self.jexjex_tickets.datetime,
                         self.jexjex_tickets.request, cacheable=True).as_list()
        return row

    def show(self):
        return self.app.db(self.jexjex_tickets).select()

    def create_models(self):
        #
        #jexjex
        if not hasattr(self.app.db, 'jexjex_tickets'):
            self.jexjex_tickets = self.app.db.define_table("jexjex_tickets",
                            self.app.field("refer", 'string'),
                            self.app.field("userip", 'string', \
                   default=self.app.request.env.get('REMOTE_ADDR', 'unknown')),
                            self.app.field("request", 'string', \
                                                         default=self.app.url),
                            self.app.field("datetime", 'datetime', \
                                                 default=self.app.request.now),
                            self.app.field("message", 'text')
                            )
        else:
            self.jexjex_tickets = self.app.db.jexjex_tickets
